$(function() {
	//获取所有小方块的集合
	var items = $(".big .item");
	let num = 0;
	let first = "first_player";
	let second = "second_player";
	//给所有小方块加上index属性
	for (let i = 0; i < items.length; i++) {
		items[i].index = i;
	}

	function jianjia(val, index, remove_class, add_class) {
		$(items[index + val]).removeClass(remove_class).addClass(add_class)
	}

	function change(i_first, i_second, index, remove_class, add_class) {
		[i_first, i_second].forEach(function(val) {
			if (!items[index + val] || items[index + val].getAttribute("background-image") == null || items[index + val].getAttribute(
					"background-image") == "second_player") {
				jianjia(val, index, remove_class, add_class);
			} else {
				jianjia(val, index, remove_class, add_class);
			}
		})
	}

	function changemiddle(index, a, b, c, d, remove_class, add_class) {
		[a, b, c, d].forEach(function(val) {
			if (!items[index + val] || items[index + val].getAttribute("background-image") == null || items[index + val].getAttribute(
					"background-image") == "second_player") {
				jianjia(val, index, remove_class, add_class);
			} else {
				jianjia(val, index, remove_class, add_class);
			}
		})
	}
	function gameover(){
		//输赢判断 0，4，8，12
		// 0，5，10，15，或0，3，6，9
	}
	// 给每个小方块添加点击事件
	items.on("click", function() {
		num++;
		let index = $(this).index();
		if (num % 2 !== 0) {
			//第一个玩家点击 先要判断背景图片是否为空
			if (index == 0 || index == 4 || index == 8 || index == 12) {
				change(-3, 5, index, second, first)
			} else if (index == 3 || index == 7 || index == 11 || index == 15) {
				change(-5, 3, index, second, first)
			} else {
				changemiddle(index, -5, -3, 3, 5, second, first);
			}
		} else {
			if (index == 0 || index == 4 || index == 8 || index == 12) {
				change(-4, 4, index, first, second)
				if (!items[index + 1] || items[index + 1].getAttribute("background-image") == null || items[index + 1].getAttribute(
						"background-image") == "first_player") {
					jianjia(1, index, first, second)
				}
			} else if (index == 3 || index == 7 || index == 11 || index == 15) {
				change(-4, 4, index, first, second)
				if (!items[index - 1] || items[index - 1].getAttribute("background-image") == null || items[index - 1].getAttribute(
						"background-image") == "first_player") {
					jianjia(-1, index, first, second)
				}
			} else {
				changemiddle(index, -4, -1, 1, 4, first, second);
			}
		}
	})
	
})
